//
//  HYGameInfoHeaderView.m
//  HaiYue
//
//  Created by skyline on 2018/5/10.
//  Copyright © 2018年 skyline. All rights reserved.
//

#import "HYGameInfoHeaderView.h"
CGFloat const SegHeight = 30;


@interface HYGameInfoHeaderView()
@property (nonatomic, strong) HYImageView *IMG;
@property (nonatomic, strong) HYImageView *discountIMG;
@property (nonatomic, strong) HYLabel *gameLBL;
@property (nonatomic, strong) HYLabel *descriptionLBL;
@property (nonatomic, strong) HYLabel *discountLBL;
@end
@implementation HYGameInfoHeaderView
- (instancetype)init
{
    self = [super init];
    if (self)
    {
        self.seg.hidden = NO;
    }
    return self;
}

- (HYImageView *)IMG
{
    if (!_IMG)
    {
        _IMG = [[HYImageView alloc]initWithImage:nil cornerRadius:10.0];
        [self addSubview:_IMG];
        [_IMG mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).with.offset(GAP);
            make.top.equalTo(self).with.offset(GAP);
            make.size.mas_equalTo(CGSizeMake(HYGameInfoIMG_Height, HYGameInfoIMG_Height));
        }];
    }
    return _IMG;
}

- (HYImageView *)discountIMG
{
    if (!_discountIMG)
    {
        _discountIMG = [[HYImageView alloc]initWithImage:[UIImage imageNamed:@"gameinfo_discount"]];
        [self addSubview:_discountIMG];
        [_discountIMG mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self);
            make.top.equalTo(self);
            make.size.mas_equalTo(CGSizeMake(HYOnlineCellIMG_Width * 0.8 * 185/259, HYOnlineCellIMG_Width * 0.8));
        }];
    }
    return _discountIMG;
}
- (HYLabel *)discountLBL
{
    if (!_discountLBL)
    {
        _discountLBL = [[HYLabel alloc]init];
        [self.discountIMG addSubview:_discountLBL];
        CGFloat offsetCenterY = HYOnlineCellIMG_Width * 0.8 / 2 - (HYOnlineCellIMG_Width * 0.8 * (259 - 75) / 259 / 2);
        [_discountLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.discountIMG);
            make.centerY.equalTo(self.discountIMG).with.offset(offsetCenterY);
        }];
    }
    return _discountLBL;
}
- (HYLabel *)gameLBL
{
    if (!_gameLBL)
    {
        _gameLBL = [[HYLabel alloc]initWithText:@"" textColor:TEXT_BLACK_COLOR textFont:[UIFont systemFontOfSize:15 weight:UIFontWeightBold]];
        [self addSubview:_gameLBL];
        [_gameLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.IMG.mas_right).with.offset(GAP);
            make.top.equalTo(self.IMG);
        }];
    }
    return _gameLBL;
}

- (HYLabel *)descriptionLBL
{
    if (!_descriptionLBL)
    {
        _descriptionLBL = [[HYLabel alloc]initWithText:@"" textColor:TEXT_GRAY_COLOR textFont:[UIFont systemFontOfSize:13]];
        _descriptionLBL.numberOfLines = 0;
        [self addSubview:_descriptionLBL];
        [_descriptionLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.IMG.mas_right).with.offset(GAP);
            make.bottom.equalTo(self.IMG);
        }];
    }
    return _descriptionLBL;
}

- (UISegmentedControl *)seg
{
    if (!_seg)
    {
        _seg = [[UISegmentedControl alloc]initWithItems:[[NSArray alloc] initWithObjects:@"详情", @"点评", @"开服", @"礼包", @"交易",nil]];
        _seg.tintColor = THEME_COLOR;
        _seg.selectedSegmentIndex = 0;
        [_seg addTarget:self action:@selector(actionForSegValueChanged) forControlEvents:UIControlEventValueChanged];
        [self addSubview:_seg];
        [_seg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.IMG.mas_bottom).with.offset(GAP);
            make.left.equalTo(self).with.offset(GAP);
            make.right.equalTo(self).with.offset(-GAP);
            make.bottom.equalTo(self).with.offset(-GAP);
        }];
    }
    return _seg;
}

- (CGRect)bounds
{
    return CGRectMake(0, 0, 0, HYGameInfoIMG_Height + SegHeight + GAP * 3.0);
}

- (void)setModel:(HYGameDetailModel *)model
{
    if (_model != model)
    {
        _model = model;
        self.IMG.imageURL = _model.icon;
        self.gameLBL.text = _model.gamename;
        self.descriptionLBL.text = [NSString stringWithFormat:@"一句话简介:\n%@",_model.simpledesc];
        if (![self.model.discount isEqualToString:@"0"])
        {
            NSString  *string = [NSString stringWithFormat:@"%@折",self.model.discount];
            NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:string];
            [attr addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0,string.length)];
            [attr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:8 weight:UIFontWeightLight] range:NSMakeRange(string.length -1, 1)];
            [attr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:11 weight:UIFontWeightBold] range:NSMakeRange(0, string.length -1)];
            self.discountLBL.attributedText = attr;
        }
    }
}


- (void)actionForSegValueChanged
{
    [self sendActionsForControlEvents:UIControlEventValueChanged];
}
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
